//
//  ZPDetailViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/6.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ZPDetailViewController.h"
#import "ZPDetailTableViewCell.h"
#import "LoginViewController.h"
#import "ZPDetailModel.h"
@interface ZPDetailViewController ()
<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView * _tableView;
    
    ZPDetailModel * myModel;
 }
@property (strong, nonatomic) FeRollingLoader *rollingLoader;


@end

@implementation ZPDetailViewController
-(void)viewWillAppear:(BOOL)animated
{    [super viewWillAppear:YES];

    
    //self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    
    _rollingLoader = [[FeRollingLoader alloc] initWithView:self.view title:@"LOADING"];
    [self.view addSubview:_rollingLoader];
    
    [_rollingLoader show];
    [self createBottomView];
    //[self addBackItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(twoBtnSectedNotice:) name:@"twoBtnSectedNotice" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT-64-SIZETO6H(82)) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ZPDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPDetailTableViewCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ZPDetailFirCell" bundle:nil] forCellReuseIdentifier:@"ZPDetailFirCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ZPDetailSecCell" bundle:nil] forCellReuseIdentifier:@"ZPDetailSecCell"];

    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestData];
}
-(void)requestData
{
    __weak typeof(self)weakSelf = self;
    [U76HONet dutyDetailWithaction:@"" dutyId:_dutyId andDataBlock:^(ZPDetailModel *model) {
        NSLog(@"职位详情获取数据成功了");
        myModel = model;
        [weakSelf addNavgationBarWithtitle:model.dutyname];
        [_tableView reloadData];
        [_rollingLoader removeFromSuperview];
        
    }];
}
-(void)resumeSelected
{
    __weak typeof(self)weakSelf = self;
    [U76HONet resumeSelectedWithAction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        NSLog(@"获取简历的id数据成功了");
        if (arr.count!=0) {
            //弹出提示框进行简历的选择
            NSMutableArray * tempArr = [[NSMutableArray alloc]init];
            NSMutableArray * tempArr2 = [[NSMutableArray alloc]init];
            for (int i = 0; i<arr.count; i++) {
                ResumeSelectedModel * model = arr[i];
                if ([model.istop isEqualToString:@"1"]) {
                    [tempArr insertObject:model.title atIndex:0];
                    [tempArr2 insertObject:model atIndex:0];
                }
                else
                {
                    [tempArr addObject:model.title];
                    [tempArr2 addObject:model];
                }
            }
            [WheelView wheelViewOneComponentWithTitle:@"请选择简历" dataArray:tempArr andSelectRow:0 Block:^(NSInteger i) {
                ResumeSelectedModel * model = tempArr2[i];
                NSLog(@"%@",model.rid);
                [weakSelf deliverResumeWithid:model.rid];
            }];
        }
    }];
}
-(void)deliverResumeWithid:(NSString *)rid
{
    [U76HONet deliverResumeWithAction:@"" jid:_dutyId rid:rid andDataBlock:^(NSDictionary *dic) {
        NSLog(@"投递简历成功到服务器了");
    }];
}
#pragma mark - _tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZPDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ZPDetailTableViewCell"];
    if (myModel!=nil) {
        cell.model = myModel;
    }
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [self createSection];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSString * contentStr;
    
    NSAttributedString * Str = [[NSAttributedString alloc] initWithData:[myModel.intro dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    contentStr = [Str string];
    CGFloat height = [contentStr heightOfSize:17 width:SCREEN_WIDTH-30];
    return height+20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
   
}

#pragma mark - createSection
-(UIView *)createSection
{
    
    if (myModel!=nil) {
        NSString * contentStr;
        
        NSAttributedString * Str = [[NSAttributedString alloc] initWithData:[myModel.intro dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        contentStr = [Str string];
        CGFloat height = [contentStr heightOfSize:17 width:SCREEN_WIDTH-30];
        
        UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(SIZETO6W(15), 10, SCREEN_WIDTH-SIZETO6W(15)*2, height)];
        
        //nameLab.translatesAutoresizingMaskIntoConstraints = NO;
        nameLab.text = contentStr;
        nameLab.numberOfLines = 0;
        nameLab.textColor = UIColorFromRGB(0x333944);

        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height + 20)];
        view.backgroundColor = [UIColor whiteColor];
        [view addSubview:nameLab];

        return view;
    }
    
    
    
    return nil;
}
-(void)createBottomView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-SIZETO6H(82), SCREEN_WIDTH, SIZETO6H(82))];
//    view.alpha = 0.9;
    view.userInteractionEnabled = YES;
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(30),view.height/2 - SIZETO6H(18), SCREEN_WIDTH-SIZETO6W(30)*2, view.height-SIZETO6H(18)*2);
    [btn setTitle:@"申请职位" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(applyAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:UIColorFromRGB(0x6EC844) forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [UIColorFromRGB(0x6EC844) CGColor];
    btn.layer.borderWidth = 1.0f;
    [view addSubview:btn];
    
    [self.view addSubview:view];
}
-(void)applyAction:(id)sender
{
//    [Singleton alerTwoBtnMessage:@"请登录" content:@"您还未登录,登录后才可发简历" leftBtnName:@"在逛逛" rightBtnName:@"登录"];
    if ([self isLogin]) {
        [self resumeSelected];
    }
}
-(void)twoBtnSectedNotice:(NSNotification *)noti
{
    NSLog(@"招聘职位的2个按钮收到了通知");
    int tempInt = [noti.object intValue];
    if (tempInt == 1) {
        
    }
    else
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        
        UINavigationController * Nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
        Nav.navigationBarHidden = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"SQZW" object:@"SQZW"];
        [self presentViewController:Nav animated:YES completion:nil];
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:loginVC];
               
                nvc.navigationBarHidden = YES;
                [self presentViewController:nvc animated:YES completion:nil];
                
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        
        UINavigationController * Nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
        Nav.navigationBarHidden = YES;
        
        [self presentViewController:Nav animated:YES completion:nil];
    }
}
-(void)addNavgationBarWithtitle:(NSString *)title
{
//    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
//    titleLab.textAlignment = NSTextAlignmentCenter;
//    titleLab.text = title;
    self.navigationItem.title = title;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
